# 银行主要业务 查看信息 取款 转账 充值
from datetime import datetime
from func import auth_login, func_choice
from func import login_user_dict, check_login, save_flow
from func import save_data, read_data


def bank_info():  # 卡号，余额，流水
    flow = save_flow('read', login_user_dict['username'], '')
    msg = f'''
    当前登录用户{login_user_dict['username']}信息如下
    卡号:{login_user_dict['bank_id']}
    余额:{login_user_dict['balance']}
    流水如下：\n
{flow}'''
    return True, msg


def check_flow():  # 查看余额
    check_login()
    data_dict = read_data('info', 'all_info.txt')
    balance = data_dict[login_user_dict['username']]['balance']
    return True, f"当前登录用户{login_user_dict['username']}，还剩余额{balance}"


@auth_login
@func_choice('recharge')
def recharge(*args, **kwargs):  # 充值
    check_login()
    data = read_data('info', 'all_info.txt')
    balance = kwargs.get('balance')
    user_new_balance = login_user_dict['balance'] + balance
    user_data = data[login_user_dict['username']]
    user_data['balance'] = user_new_balance
    flag, msg = save_data('info', username=login_user_dict['username'], **user_data)
    if flag:
        txt = f"充值{balance}元成功"
        save_flow('save', user=login_user_dict['username'],
                  txt=f"{txt}>>>时间为>>>{datetime.now().strftime('%Y年%m月%d日 %H时%M分%S秒')}")
        return flag, txt
    else:
        return flag, msg


@auth_login
@func_choice('withdraw')
def withdraw_money(**kwargs):  # 取款
    data = read_data('info', 'all_info.txt')
    balance = kwargs.get('balance')
    user_new_balance = login_user_dict['balance'] - balance
    user_data = data[login_user_dict['username']]
    user_data['balance'] = user_new_balance
    flag, msg = save_data('info', username=login_user_dict['username'], **user_data)
    if flag:
        txt = f"{login_user_dict['username']}取款{balance}元成功"
        save_flow('save', user=login_user_dict['username'],
                  txt=f"{txt}>>>时间为>>>{datetime.now().strftime('%Y年%m月%d日 %H时%M分%S秒')}")
        return flag, txt
    else:
        return flag, msg


@auth_login
@func_choice('transfer')
def transfer_money(**kwargs):  # 转账
    data = read_data('info', 'all_info.txt')
    balance = kwargs.get('balance')  # 转账金额
    for_bank_id = kwargs.get('bank_id')  # 收集到转账银行卡号
    user_new_balance = login_user_dict['balance'] - balance  # 所剩余额
    pay_dict = {'id': login_user_dict['bank_id'], 'pay_pwd': login_user_dict['bank_pay_pwd'],
                'balance': user_new_balance}
    save_data('info', username=login_user_dict['username'], **pay_dict)
    payee_dict = {}  # 收款人信息
    username = ''  # 收款人姓名
    for key, value in data.items():
        if for_bank_id == value['id']:
            username = key
            payee_dict[key] = value
    payee_dict[username]['balance'] += balance
    save_info = payee_dict[username]
    flag, msg = save_data('info', username=username, **save_info)
    if flag:
        txt = f"{login_user_dict['username']}成功转账给{username}>>>{balance}元"
        save_flow('save', user=login_user_dict['username'],
                  txt=f"{txt},时间为>>>{datetime.now().strftime('%Y年%m月%d日 %H时%M分%S秒')}")
        save_flow('save', user=username, txt=f"{txt},时间为>>>{datetime.now().strftime('%Y年%m月%d日 %H时%M分%S秒')}")
        return flag, txt
    else:
        return flag, msg
